//
//  UKRequestAgent.m
//  ZMUke
//
//  Created by liqian on 2018/12/6.
//  Copyright © 2018 zmlearn. All rights reserved.
//

#import "UKRequestAgent.h"
#import "UKBaseRequest.h"
#import "Macro.h"
#import "HDUkeInfoCenter.h"
#import "LHDAFHTTPSessionManager.h"

@interface UKRequestAgent ()
@property (nonatomic, strong) LHDAFHTTPSessionManager *manager;
@end

@implementation UKRequestAgent

- (void)addRequest:(UKBaseRequest *)request {
    NSLog(@"BaseUrl:=====%@",self.manager.baseURL);
    if (self.manager.requestSerializer.timeoutInterval != request.timeoutInterval) {
        self.manager.requestSerializer.timeoutInterval = request.timeoutInterval;
    }

    if ([HDUkeInfoCenter sharedCenter].token.length > 0) {
        [_manager.requestSerializer  setValue:[HDUkeInfoCenter sharedCenter].token forHTTPHeaderField:@"Authorization"];
    }
    

    
    switch (request.requestMethod) {
        case UKRequestMethodGET: {

            NSURLSessionDataTask *dataTask;
            
            dataTask = [self.manager GET:request.requestURL parameters:request.parameters headers:nil progress:request.progressBlock success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                !request.successBlock ?: request.successBlock(responseObject);
                [request clearBlock];
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                !request.failureBlock ?: request.failureBlock(error);
                [request clearBlock];
            }];
            request.sessionTask = dataTask;
        } break;
            
        case UKRequestMethodPOST: {

            NSURLSessionDataTask *dataTask;
            
            dataTask = [self.manager POST:request.requestURL parameters:request.parameters headers:nil progress:request.progressBlock success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                !request.successBlock ?: request.successBlock(responseObject);
                [request clearBlock];
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                !request.failureBlock ?: request.failureBlock(error);
                [request clearBlock];
            }];
            request.sessionTask = dataTask;
        } break;
         
        case UKRequestMethodDELETE: {
            NSURLSessionDataTask *dataTask;
            
            dataTask = [self.manager DELETE:request.requestURL parameters:request.parameters headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                !request.successBlock ?: request.successBlock(responseObject);
                [request clearBlock];
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                !request.failureBlock ?: request.failureBlock(error);
                [request clearBlock];
            }];
            request.sessionTask = dataTask;
        } break;
            
        case UKRequestMethodPUT: {
            NSURLSessionDataTask *dataTask;
            
            dataTask = [self.manager PUT:request.requestURL parameters:request.parameters headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                !request.successBlock ?: request.successBlock(responseObject);
                [request clearBlock];
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                !request.failureBlock ?: request.failureBlock(error);
                [request clearBlock];
            }];
            request.sessionTask = dataTask;
        } break;
        default: {
            
        } break;
    }
}

- (void)settingconfigNetWorkManger {
    if (!self.manager) {
        NSURL *url = [NSURL URLWithString:[HDUkeInfoCenter sharedCenter].HTTPURL];
        self.manager = [[LHDAFHTTPSessionManager alloc] initWithBaseURL:url];
        self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
        [self.manager.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/plain", nil];
    }
}
+ (instancetype)sharedAgent {
    return [[self alloc] init];
}

- (instancetype)init {
    if (self = [super init]) {
        [self settingconfigNetWorkManger];
    }
    return self;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static UKRequestAgent *singleClass = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleClass = [super allocWithZone:zone];//最先执行，只执行了一次
    });
    return singleClass;
}
@end
